package com.jerry.huihome.module.find.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.jerry.huihome.base.BaseApplication;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

/**
 * Created by Administrator on 2016/5/12.
 */
public class GirdViewListImageAdapter extends BaseAdapter {

    private  Context context;
    private  List<String> list;
    private DisplayImageOptions options;
    private int screenWidth;
    public GirdViewListImageAdapter(){}
    public GirdViewListImageAdapter(Context context,List<String> list){
        this.list = list;
        this.context = context;
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager windowManager =
                (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);
        // 屏幕宽度
        screenWidth = metrics.widthPixels;
        options = new DisplayImageOptions.Builder().cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .build();
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public String getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            ImageView imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams((int) (screenWidth/3.5), (int) (screenWidth/3.5)));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            String item = getItem(position);
            BaseApplication.getApplication().getImageLoader().displayImage(item,imageView,options);
            convertView = imageView;
        }
        return convertView;
    }
}
